import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/co_chemicals_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';

///危险化学品页面
class HazardousChemicalsController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 筛选列表
  List<FilterBean> filterList = [];

  /// 搜索条件
  String? search;

  /// 选中的危险化学品种类
  String? chemicalsType;

  @override
  void onInit() {
    super.onInit();
    init();
  }

  Future<void> init() async {
    // 获取后端类型字典
    final List<GetDictAllEntity> dictTypeList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'chemicals_type_l3yzr5Ox');

    final List<FilterEntitiesBean> typeList = [];
    // 添加类型筛选条件
    for (var item in dictTypeList) {
      typeList.add(FilterEntitiesBean(name: item.dictionaryLabel!, value: item.id!));
    }
    //筛选控制bean
    final FilterBean filterBean = FilterBean(title: '类型', filterList: typeList);
    filterList.add(filterBean);
  }

  /// 获取列表数据
  Future<List<CoChemicalsEntity>> getListData(int pageNum, int pageSize) async {
    // 获取产品列表接口
    final res = await HttpUtil().generateGetRequest<List<CoChemicalsEntity>>(
      Interface.getPageChemical,
      queryParameters: {
        'pageNum': pageNum,
        'pageSize': pageSize,
        'chemicalName': search,
        'chemicalsType': chemicalsType,
      },
    ).execute();
    List<CoChemicalsEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  //筛选按钮
  void onFilterButtonPressed() {
    popRightScaffoldKey.currentState?.openEndDrawer();
  }

  // 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    chemicalsType = null;
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '类型') {
        chemicalsType = i.chosenValue[0].value as String;
      }
    }
    update();
    refreshPage();
  }

  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

}
